import java.io.*;
import java.net.*;
import java.util.ArrayList;

public class HW1 {
    public static void main(String[]args) throws Exception {
//TODO: Maybe spin this into a separate method, return the stored arraylist; also maybe store a question number and filter accordingly in the initial reading
/*
    Request the page, store relevant lines into memory
*/
        URL url = new URL("https://www.cia.gov/library/publications/the-world-factbook/geos/countrytemplate_au.html");
        URLConnection u = url.openConnection();
        BufferedReader br = new BufferedReader(new InputStreamReader(u.getInputStream()));
        String out = br.readLine();
        ArrayList<String> pageSource = new ArrayList<String>();
        boolean isInArticleBody = false;
        int numJavascriptTagsPassed = 0; //This should be 2 before we start caring about what the page contains
        int count = 0;
        while(out != null) {
            while(!isInArticleBody) { //Skip beginning parts of source until we hit divs containing information
                if(out == null) {//Make sure we don't run out of HTML source prematurely
                    System.out.println("Exiting early, ran out of HTML");
                    System.exit(0);
                }
                if(out.contains("</script")) { //The divs containing information all occur after the second set of javascript tags
                    numJavascriptTagsPassed++;
                    if(numJavascriptTagsPassed == 2) {
                        isInArticleBody = true;
                    }
                }
                out = br.readLine();
            }
            out = out.trim(); //Remove whitespace, don't bother printing blank lines
            out = out.replaceAll("\\<.*?\\>", ""); //Remove tags, regex pulled from http://www.dzone.com/snippets/java-regex-remove-html-tags
            if(!out.equals("")) {
                pageSource.add(out);
                //System.out.println(out);
            }
            out = br.readLine();
            count++;
        }
        br.close();
        System.out.println("Number of lines stored = " + count);


//TODO: Generalize, set up with prompts to user, remove debug text
//Tests for each question
        getNaturalDisasterVulnerability("Earthquake", pageSource);
        getNumberOfPoliticalParties(pageSource);
        isColorInCountryFlag("WHite", pageSource);
        boolean checkq4 = isCountryLandlockedByOnlyOneCountry(pageSource);
        System.out.println("Q4 answer- " + checkq4);


        System.exit(0);
    }

    public static ArrayList<String> getCountryData(String urlString) {
        
    }

/*
    See if a particular country is vulnerable to a particular natural disaster
*/
    public static void getNaturalDisasterVulnerability(String disaster, ArrayList<String> countryData) {
        disaster = disaster.toLowerCase();
        for(int i = 0; i<countryData.size(); i++) {
            String s = countryData.get(i);
            if(s.toLowerCase().contains("natural hazards")) {
                String hazardData = countryData.get(i+1);
                if(hazardData != null) {
                    if(hazardData.toLowerCase().contains(disaster)) {
                        System.out.println("Yes"); // Change later
                    }
                    System.out.println(hazardData);
                }
                return;
            }
        }
    }

/*
    See how many political parties exist in a country
*/
    public static void getNumberOfPoliticalParties(ArrayList<String> countryData) {
        for(int i = 0; i<countryData.size(); i++) {
            String s = countryData.get(i);
            if(s.toLowerCase().contains("political parties and leaders")) {
                String partyData = countryData.get(i+1);
                if(partyData != null) {
                    System.out.println(partyData);
                    String[] splitBySemiColon = partyData.split(";");
                    System.out.println("Number of political parties in country = " + splitBySemiColon.length);
                }
                return;
            }
        }
    }

/*
    See if a particular color is in a country's flag
*/
    public static void isColorInCountryFlag(String color, ArrayList<String> countryData) {
        color = color.toLowerCase();
        for(int i = 0; i<countryData.size(); i++) {
            String s = countryData.get(i);
            if(s.toLowerCase().contains("flag description")) {
                String flagData = countryData.get(i+1);
                flagData = flagData.split(";")[0]; //We don't care about the story behind the flag colors, just the first part that actually describes the flag's appearance
                if(flagData != null) {
                    if(flagData.toLowerCase().contains(color)) {
                        System.out.println("Yes");
                    }
                    System.out.println(flagData);
                }
                return;
            }
        }
    }

/*
    See if a country is landlocked and shares borders with only one other country
*/
    public static boolean isCountryLandlockedByOnlyOneCountry(ArrayList<String> countryData) {
        for(int i = 0; i<countryData.size(); i++) {
            String s = countryData.get(i);
            if(s.toLowerCase().contains("land boundaries")) { //Check if the country borders only one other country.  This comes first in the page layout.
                String borderLength = countryData.get(i+1);
                if(borderLength.equalsIgnoreCase("0 km")) { //Country has no borders with other countries, so we're done checking
                    return false;
                }
                String borderCountryData = countryData.get(i+3); // Line i + 1 contains the total border distance, not the countries that this country borders
                if(borderCountryData != null) {
                    System.out.println(borderCountryData);
                    if((borderCountryData.split(",")).length == 1) {
                        System.out.println("Yes, borders one country");
                        return true;
                    }
                    else {
                        return false;
                    }
                }
                else {
                    return false;
                }
            }
            if(s.toLowerCase().contains("coastline")) { //Check if the country borders only one other country.  This comes first in the page layout.
                String coastLength = countryData.get(i+1);
                if(coastLength != null) {
                    return false;
                }
                System.out.println("Coastline length- " + coastLength);
                if(coastLength.equalsIgnoreCase("0 km")) { //Country has no borders with other countries, so we're done checking
                    System.out.println("Yes");
                    return true;
                }
            }
        }
        return false;
    }


}
